<?php
class ModelUserUserLog extends Model {
	public function getUserLogs($filter) {
		$sql = "SELECT * FROM @@user_log WHERE 1";
		
		if ($filter->get('filter_username')) {
			$sql .= " AND username = '" . ES($filter->get('filter_username')) . "'";
		}
		if ($filter->get('filter_ip')) {
			$sql .= " AND ip = '" . ES($filter->get('filter_ip')) . "'";
		}
		if ($filter->get('filter_date_added')) {
			$sql .= " AND DATE(date_added) = '" . ES($filter->get('filter_date_added')) . "'";
		}
		$sql .= $filter->sort('user_log_id', 'DESC');
		$sql .= $filter->limit();
		
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getTotalUserLogs($filter) {
		$sql = "SELECT COUNT(*) FROM @@user_log WHERE 1";
		
		if ($filter->get('filter_username')) {
			$sql .= " AND username = '" . ES($filter->get('filter_username')) . "'";
		}
		if ($filter->get('filter_ip')) {
			$sql .= " AND ip = '" . ES($filter->get('filter_ip')) . "'";
		}
		if ($filter->get('filter_date_added')) {
			$sql .= " AND DATE(date_added) = '" . ES($filter->get('filter_date_added')) . "'";
		}
		
		return $this->db->queryOne($sql);
	}
	
	public function clearUserLogs($older_than = 20) {
		return $this->db->runSql("DELETE FROM @@user_log WHERE DATE_SUB(CURDATE(),INTERVAL $older_than DAY) > date_added");
	}
}